package com.zlm.user.emp.controller;

import com.zlm.common.base.BaseController;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.dto.userEmp.EmpResumeDto;
import com.zlm.common.dto.userEmp.EmpResumeParamsDto;
import com.zlm.common.dto.userEmp.EmpResumeResponseDto;
import com.zlm.common.entity.*;
import com.zlm.common.enums.SalaryRangeEnum;
import com.zlm.common.enums.WorkTypeEnum;
import com.zlm.user.emp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 简历
 * @Author: duyufu
 * @Date: 2019/5/15 15:40
 */
@RestController
@RequestMapping("/resume")
public class EmpResumeController extends BaseController {

    @Autowired
    private EmpResumeService empResumeService;

    @Autowired
    private SysIndustryService sysIndustryService;

    @Autowired
    private SysTimeSectionService sysTimeSectionService;

    @GetMapping("/getEmpResume")
    public JsonResultDto getEmpResume(Integer empId) {
        try {
            EmpResumeResponseDto empResumeDto = empResumeService.getResume(empId);
            return success(empResumeDto);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    @PostMapping("/saveEmpResume")
    public JsonResultDto saveEmpResume(@RequestBody EmpResumeParamsDto empResumeParamsDto) {
        try {
            empResumeService.saveEmpResume(empResumeParamsDto);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    @GetMapping("/getEmpPostResume")
    public JsonResultDto getEmpPostResume(Integer empId) {
        try {
            //获取用户简历信息
            EmpResumeDO empResumeDO = empResumeService.queryResumeById(empId);
            List<Map<String,Object>> industryInfo = new ArrayList<>();
            List<SysTimeSectionDO> sysTimeSections = new ArrayList<>();
            List<SysTimeSectionDO> trainTimeSections = new ArrayList<>();

            //根据简历获取期望行业
            if (null != empResumeDO) {
                //查询期望行业
                List<Integer> industryList = new ArrayList<>();
                if (empResumeDO.getIndustry() != null && !"".equals(empResumeDO.getIndustry())) {
                    industryList.add(empResumeDO.getIndustry());
                }
                if (empResumeDO.getIndustry1() != null && !"".equals(empResumeDO.getIndustry1())) {
                    industryList.add(empResumeDO.getIndustry1());
                }
                if (empResumeDO.getIndustry2() != null && !"".equals(empResumeDO.getIndustry2())) {
                    industryList.add(empResumeDO.getIndustry2());
                }
                if (!industryList.isEmpty()) {
                    List<Map<String, Object>> industryInfoResult = sysIndustryService.queryIndustryList(industryList);
                    if (industryInfoResult.size() > 0) {
                        industryInfo = industryInfoResult;
                    }
                }
                SysTimeSectionDO sysTimeSection = new SysTimeSectionDO();
                sysTimeSection.setResumeId(empResumeDO.getResumeId());
                sysTimeSection.setAlive(1);
                //工作时间
                sysTimeSection.setIdentitys(1);
                sysTimeSections = sysTimeSectionService.queryTimeSection(sysTimeSection);
                //培训时间
                sysTimeSection.setIdentitys(2);
                trainTimeSections = sysTimeSectionService.queryTimeSection(sysTimeSection);
            } else {
                empResumeDO = new EmpResumeDO();
            }
            //封装返回对象
            EmpResumeDto empResumeDto = new EmpResumeDto();
            empResumeDto.setIndustryInfo(industryInfo);
            empResumeDto.setProvinceName(empResumeDO.getProvinceName());
            empResumeDto.setCityName(empResumeDO.getCityName());
            empResumeDto.setAreaName(empResumeDO.getAreaName());
            empResumeDto.setResumeStyle(empResumeDO.getResumeStyle());
            empResumeDto.setResumeStyleName(empResumeDO.getResumeStyle() == null ? "" :
                    WorkTypeEnum.getNameByCode(empResumeDO.getResumeStyle()));
            empResumeDto.setSkills(empResumeDO.getSkills());
            empResumeDto.setSalary(empResumeDO.getSalary());
            empResumeDto.setSalaryName(empResumeDO.getSalary() == null ? "" :
                    SalaryRangeEnum.getNameByCode(empResumeDO.getSalary()));
            empResumeDto.setPostId(empResumeDO.getPostId());
            empResumeDto.setPostName(empResumeDO.getPostName());
            empResumeDto.setPostId1(empResumeDO.getPostId1());
            empResumeDto.setPostName1(empResumeDO.getPostName1());
            empResumeDto.setPostId2(empResumeDO.getPostId2());
            empResumeDto.setPostName2(empResumeDO.getPostName2());
            empResumeDto.setSysTimeSections(sysTimeSections);
            empResumeDto.setTrainTimeSections(trainTimeSections);
            return success(empResumeDto);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }
}
